﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;
using RBOSS.Business.ReportsModule;
using System.Data;

namespace RBOSS.WebUI
{
    public partial class ProductsReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Helper hlpr = new Helper();
                List<Category> categoriesList = hlpr.GetCategories();

                Category cat = new Category();
                cat.categoryId = -1;
                cat.categoryName = "All";

                categoriesList.Insert(0, cat);

                ddlCategories1.DataSource = categoriesList;
                ddlCategories1.DataBind();

                ddlCategories2.DataSource = categoriesList;
                ddlCategories2.DataBind();

                DateTime dt1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                DateTime dt2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month));

                txtStartDate1.Text = dt1.ToString("MM/dd/yyyy");
                txtEndDate1.Text = dt2.ToString("MM/dd/yyyy");
                txtStartDate2.Text = dt1.ToString("MM/dd/yyyy");
                txtEndDate2.Text = dt2.ToString("MM/dd/yyyy");
                updateUI();
            }
        }

        private void FillChart1(DataTable dt)
        {
            this.Chart1.Titles[0].Text = "Best Selling Products";
            this.Chart1.ViewStateContent = SerializationContents.All;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == 5)
                    break;
                Chart1.Series["RankingValues"].Points.AddXY((i + 1), int.Parse(dt.Rows[i]["Quantity"].ToString()));
                Chart1.Series["RankingValues"].Points[i].AxisLabel = dt.Rows[i]["ProductName"].ToString() + " (" + dt.Rows[i]["Amount"].ToString() + "$)";
            }

            this.Chart1.Series[0].ToolTip = "#VAL{#,##0}";

            Chart1.Series["RankingValues"]["PixelPointWidth"] = "30";

            Chart1.Series["RankingValues"]["PointWidth"] = "0.6";

            //// Set drawing style
            Chart1.Series["RankingValues"]["DrawingStyle"] = "Cylinder";

            foreach (System.Web.UI.DataVisualization.Charting.DataPoint dp in Chart1.Series[0].Points)
                dp.Color = Color.Orange;
        }

        private void FillChart2(DataTable dt)
        {
            this.Chart2.Titles[0].Text = "Least Selling Products";
            this.Chart2.ViewStateContent = SerializationContents.All;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == 5)
                    break;
                Chart2.Series["RankingValues"].Points.AddXY((i + 1), int.Parse(dt.Rows[i]["Quantity"].ToString()));
                Chart2.Series["RankingValues"].Points[i].AxisLabel = dt.Rows[i]["ProductName"].ToString() + " (" + dt.Rows[i]["Amount"].ToString() + "$)";
            }

            this.Chart2.Series[0].ToolTip = "#VAL{#,##0}";

            Chart2.Series["RankingValues"]["PixelPointWidth"] = "30";

            Chart2.Series["RankingValues"]["PointWidth"] = "0.6";

            //// Set drawing style
            Chart2.Series["RankingValues"]["DrawingStyle"] = "Cylinder";

            foreach (System.Web.UI.DataVisualization.Charting.DataPoint dp in Chart2.Series[0].Points)
                dp.Color = Color.Navy;
        }

        protected void btnUpdate1_Click(object sender, EventArgs e)
        {
            updateUI();
        }

        protected void btnUpdate2_Click(object sender, EventArgs e)
        {
            updateUI();
        }

        public void updateUI()
        {
            Helper hlpr = new Helper();
            DataTable dt1 = hlpr.getTopProducts(DateTime.Parse(txtStartDate1.Text), (DateTime.Parse(txtEndDate1.Text)), int.Parse(ddlCategories1.SelectedValue));
            DataTable dt2 = hlpr.getLeastProducts(DateTime.Parse(txtStartDate2.Text), (DateTime.Parse(txtEndDate2.Text)), int.Parse(ddlCategories2.SelectedValue));
            FillChart1(dt1);
            FillChart2(dt2);
        }

        protected void btnExport1_Click(object sender, EventArgs e)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "Openwindow", "<script type=\"text/javascript\" >" +
                    "window.open('ReportView.aspx?FormType=TopProducts&StartDate=" + txtStartDate1.Text + "&EndDate=" + txtEndDate1.Text +
                    "&CategoryId=" + ddlCategories1.SelectedValue + "&CategoryName=" + ddlCategories1.SelectedItem.Text + "');</script>", false);
            updateUI();
        }

        protected void btnExport2_Click(object sender, EventArgs e)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "Openwindow", "<script type=\"text/javascript\" >" +
                    "window.open('ReportView.aspx?FormType=LeastProducts&StartDate=" + txtStartDate2.Text + "&EndDate=" + txtEndDate2.Text +
                    "&CategoryId=" + ddlCategories2.SelectedValue + "&CategoryName=" + ddlCategories2.SelectedItem.Text + "');</script>", false);
            updateUI();
        }
    }
}